﻿using AutoMapper;
using MediatR;
using WMS.API.Application.Command.BasicManagement.ProductManage;
using WMS.Domain.Entities.BasicManagement;
using WMS.ErrorCode;
using WMS.Interfactructure.Interfaces;

namespace WMS.API.Application.CommandHandlers.BasicManagement.ProductManage
{
    public class AddProductManageCommandHandler : IRequestHandler<AddProductManageCommand, APIResult<int>>
    {
        private readonly IBaseRepository<ProductManageModel> _productManageModelRepository;
        private readonly IMapper mapper;

        public AddProductManageCommandHandler(IBaseRepository<ProductManageModel> productManageModelRepository, IMapper mapper)
        {
            _productManageModelRepository = productManageModelRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加产品管理命令处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(AddProductManageCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var productManageModel = mapper.Map<ProductManageModel>(request);
                var result = await _productManageModelRepository.AddAsync(productManageModel);             
                return result > 0 ? await Task.FromResult(new APIResult<int>("产品添加成功") { Code= ResultCode .Success}) : await Task.FromResult(new APIResult<int>("产品添加失败"));
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
